package cn.bgs.controller;

import cn.bgs.exception.PhoenixException;
import cn.bgs.pojo.Grade;
import cn.bgs.pojo.dormitory;
import cn.bgs.service.dormitoryService;
import cn.bgs.utils.MyContent;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ProjectName: bei
 * @Package: cn.bgs.controller
 * @ClassName: dormitory
 * @Author: Administrator
 * @Description:
 * @Date: 2021/8/11 0011 19:43
 * @Version: 1.0
 */
@Controller
public class DormitoryController {
    @Autowired
    private dormitoryService dormitoryService;
    @RequestMapping("/dormitoryHtml")
    public String dormitoryHtml(){
        return "dormitory";
    }
    @RequestMapping("/findormitory")
    @ResponseBody
    public Map<String,Object> findormitory(@RequestParam(required=false,defaultValue="1") int page,
                                           @RequestParam(required=false,defaultValue="7") int limit,
                                           String area,String buildingNo,String buildingFloor,String dormitoryName,String bedNumber,String dormSupervisor){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<dormitory> dormitories = dormitoryService.findormitory(area,bedNumber,buildingFloor,buildingNo,dormitoryName,dormSupervisor);
        PageInfo<dormitory> rolePageInfo = new PageInfo<>(dormitories);
        Map<String,Object> map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        return map;
    }
    @RequestMapping("/area")
    @ResponseBody
    public List<dormitory>area(){
        return dormitoryService.area();
    }
    @RequestMapping("/insertdormitory")
    @ResponseBody
    public Integer insertdormitory(@RequestBody String istDate){
        JSONObject jsonObject = JSONObject.parseObject(istDate);
        dormitory dormitory = new dormitory();
        dormitory.setArea(jsonObject.getString("area"));
        dormitory.setBuildingFloor(jsonObject.getString("buildingFloor"));
        dormitory.setBuildingNo(jsonObject.getString("buildingNo"));
        dormitory.setDormitoryName(jsonObject.getString("dormitoryName"));
        dormitory.setBedNumber(jsonObject.getString("bedNumber"));
        dormitory.setDormSupervisorId(jsonObject.getString("dormSupervisorId"));
        dormitory.setDormSupervisor(jsonObject.getString("dormSupervisor"));
        dormitory.setUseStatus(jsonObject.getString("useStatus"));
        dormitory.setDeleteFlag("0");
        Calendar now = Calendar.getInstance();//获取时间
        dormitory.setCreateTime(now.getTime());
        dormitory.setUpdateTime(now.getTime());
        return dormitoryService.insertdormitory(dormitory);
    }
    @RequestMapping("/deleteByPkId")
    @ResponseBody
    public Integer deleteByPkId(@RequestBody String delData){
        JSONObject jsonObject = JSONObject.parseObject(delData);
        JSONArray ids =jsonObject.getJSONArray("pkIds");
        Integer[] pkIds =new Integer[ids.size()];
        for(int i=0;i<ids.size();i++){
            Object id = ids.get(i);
            pkIds[i]=Integer.parseInt(id.toString());
        }
        List<Integer> list = Arrays.asList(pkIds);
        return dormitoryService.deleteByPkId(list);
    }
    @RequestMapping("/dormitorydel")
    @ResponseBody
    public Integer dormitorydel(@RequestBody String dormitory){
        JSONObject jsonObject = JSONObject.parseObject(dormitory);
        Integer pkId =Integer.parseInt(jsonObject.getString("pkId"));
        return dormitoryService.dormitorydel(pkId);
    }
    @RequestMapping("/updateByPkId")
    @ResponseBody
    public Integer updateByPkId(@RequestBody String upDate){
        JSONObject jsonObject = JSONObject.parseObject(upDate);
        dormitory dormitory = new dormitory();
        dormitory.setPkId(jsonObject.getInteger("pkId"));
        dormitory.setArea(jsonObject.getString("area"));
        dormitory.setBuildingFloor(jsonObject.getString("buildingFloor"));
        dormitory.setBuildingNo(jsonObject.getString("buildingNo"));
        dormitory.setDormitoryName(jsonObject.getString("dormitoryName"));
        dormitory.setBedNumber(jsonObject.getString("bedNumber"));
        dormitory.setDormSupervisorId(jsonObject.getString("dormSupervisorId"));
        dormitory.setDormSupervisor(jsonObject.getString("dormSupervisor"));
        dormitory.setUseStatus(jsonObject.getString("useStatus"));
        dormitory.setDeleteFlag("0");
        Calendar now = Calendar.getInstance();//获取时间
        dormitory.setCreateTime(now.getTime());
        dormitory.setUpdateTime(now.getTime());
        return dormitoryService.updateByPkId(dormitory);
    }
    @RequestMapping("/dormitoryFind/{pkId}")
    public String dormitoryFind(@PathVariable Integer pkId, Model m){

        dormitory dormitory= dormitoryService.dormitoryFind(pkId);
        m.addAttribute("dormitory",dormitory);
        return "/dormitoryStudentList";
    }
    @RequestMapping("/dormitorys/{pkId}")
    public String dormitorys(@PathVariable Integer pkId,Model m){

        dormitory dormitory = dormitoryService.dormitoryFind(pkId);
        m.addAttribute("dormitorys",dormitory);
        return "/dormitoryStudent";
    }
    @RequestMapping("/dormitoryFindSel/{dormitoryName}")
    public String dormitoryFindSel(@PathVariable String dormitoryName,Model m){

        dormitory dormitory = dormitoryService.dormitoryFindSel(dormitoryName);
        m.addAttribute("dormitorys",dormitory);
        return "/dormitoryStudent";
    }
    @ResponseBody
    @RequestMapping("/dormitoryCount/{dormitoryId}")
    public Integer gradeCount(@PathVariable String dormitoryId){

        return dormitoryService.dormitoryCount(dormitoryId);
    }
    @RequestMapping("/dormitoryAddStu")
    @ResponseBody
    public Map<String, Object> dormitoryAddStu(@RequestBody String dormitoryAdd){

        JSONObject jsonObject = JSONObject.parseObject(dormitoryAdd);
        JSONArray jsonArray=jsonObject.getJSONArray("stuIds");
        String dormitoryId=jsonObject.getString("dormitoryId");
        String dormitoryName=jsonObject.getString("dormitoryName");
        Integer[] ids=new Integer[jsonArray.size()];
        for(int i=0;i<jsonArray.size();i++){
            Object o=jsonArray.get(i);
            ids[i]=Integer.parseInt(o.toString());
        }
        Map<String,Object> m=new HashMap<>();
        try {
            Integer count = dormitoryService.dormitoryAddStu(ids,dormitoryId,dormitoryName);
            m.put(MyContent.CODE, "0000");
            m.put(MyContent.MSG, "添加成功");
            m.put("data", count);

        } catch (PhoenixException e) {
            e.printStackTrace();
            e.printStackTrace();
            m.put(MyContent.CODE, e.getCode());
            m.put(MyContent.MSG, e.getMsg());
            m.put("data", 0);
        }

        return m;
    }
}
